<template>
  <el-form ref="form"  :rules="formRules" :model="exam" label-width="80px" label-position="left">
    <el-form-item label="题目" prop="title">
      <el-input type="text" v-model="exam.title" ></el-input>
    </el-form-item>
    <el-form-item label="答案" prop="answer">
      <el-input type="text" v-model="exam.answer"></el-input>
    </el-form-item>
    <el-form-item label="得分" prop="score">
      <el-input type="text" v-model="exam.score"></el-input>
    </el-form-item>
    <el-form-item>
      <el-button type="warning" @click="insertExam()">添加</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
    export default {
      name: "InsertNews",
      data(){
        let checkScore=(rule,value,callback)=>{
          console.log(value)
          if(value==null){
            callback(new Error("分数不能为空"))
          }else{
            callback()
          }
        }
          return{
           exam:{},
            formRules:{
              title:[{required:true,message:"题目不能为空",trigger:"blur"}],
              answer:[{required:true,message:"答案不能为空",trigger:"blur"}],
              score:[{ validator:checkScore,trigger:"blur"}]
            }
          }
      },
      methods:{
        insertExam(){
          this.$refs["form"].validate((va)=>{
            if(va){
              this.axios({
                method:"POST",
                url:"/exams",
                data:this.exam
              }).then((res)=>{
               this.$message("添加成功")
                this.$router.push("/showExam")
              })
            }
            return va;
          })

        }
      }
    }
</script>

<style scoped>

</style>
